unit uRotinas;

interface

uses Windows, Messages, SysUtils, ShellAPI, Forms, StrUtils;

type
   TRotinas = class
      private

      public
         class procedure CarregaParametros;
         class procedure GravaParametros;
         class procedure GravaParametro(pParametro, pValor: string);

         class procedure ShellExecute(pArquivo: string);

         class function GetFullPath(pPath: string): string;
   end;

implementation

uses uDMConexao, uGlobal, uErros, uDB;

{ TRotinas }

class procedure TRotinas.CarregaParametros;
begin
   PARAMETROS.LocalHinario := '.\media\hinario';
   PARAMETROS.LocalMidia := '.\media\';
   PARAMETROS.Skin := 'Office2013White';

   dmConexao.qrParametros.Open;
   try
      if dmConexao.qrParametros.Locate('CHAVE', PARAM_LOCAL_HINARIO, []) then
         PARAMETROS.LocalHinario := dmConexao.qrParametrosVALOR.AsString;

      if dmConexao.qrParametros.Locate('CHAVE', PARAM_LOCAL_MIDIA, []) then
         PARAMETROS.LocalMidia := dmConexao.qrParametrosVALOR.AsString;

      if dmConexao.qrParametros.Locate('CHAVE', PARAM_SKIN, []) then
         PARAMETROS.Skin := dmConexao.qrParametrosVALOR.AsString;
   finally
      dmConexao.qrParametros.Close;
   end;
end;

class function TRotinas.GetFullPath(pPath: string): string;
begin
   if pPath <> '' then begin
      if RightStr(pPath, 1) <> '\' then
         pPath := pPath + '\';
   end;
   Result := pPath;
end;

class procedure TRotinas.GravaParametro(pParametro, pValor: string);
begin
   clDB.StartTransaction;
   try
      dmConexao.qrParametros.Open;
      try
         if dmConexao.qrParametros.Locate('CHAVE', pParametro, []) then dmConexao.qrParametros.Edit
         else                                                           dmConexao.qrParametros.Insert;
         dmConexao.qrParametrosCHAVE.AsString := pParametro;
         dmConexao.qrParametrosVALOR.AsString := pValor;
         dmConexao.qrParametros.Post;
      finally
         dmConexao.qrParametros.Close;
      end;
      clDB.CommitTransaction;
   except on E: Exception do
      begin
         clDB.RollbackTransaction;
         raise;
      end;
   end;
end;

class procedure TRotinas.GravaParametros;
begin
   TRotinas.GravaParametro(PARAM_LOCAL_HINARIO, PARAMETROS.LocalHinario);
   TRotinas.GravaParametro(PARAM_LOCAL_MIDIA, PARAMETROS.LocalMidia);
   TRotinas.GravaParametro(PARAM_SKIN, PARAMETROS.Skin);
end;

class procedure TRotinas.ShellExecute(pArquivo: string);
var
   hRet: HINST;
begin
   try
      hRet := ShellAPI.ShellExecute(0, 'open', PWideChar(pArquivo), nil, PWideChar(ExtractFileDir(pArquivo)), SW_NORMAL);

      if hRet <= HINSTANCE_ERROR then begin
         if      hRet = 0  then
            raise Exception.Create('The operating system is out of memory or resources.')
         else if hRet = 2  then
            raise Exception.Create('The specified file was not found')
         else if hRet = 3  then
            raise Exception.Create('The specified path was not found.')
         else if hRet = 5  then
            raise Exception.Create('Windows 95 only: The operating system denied access to the specified file')
         else if hRet = 8  then
            raise Exception.Create('Windows 95 only: There was not enough memory to complete the operation.')
         else if hRet = 10 then
            raise Exception.Create('Wrong Windows version')
         else if hRet = 11 then
            raise Exception.Create('The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)')
         else if hRet = 12 then
            raise Exception.Create('Application was designed for a different operating system')
         else if hRet = 13 then
            raise Exception.Create('Application was designed for MS-DOS 4.0')
         else if hRet = 15 then
            raise Exception.Create('Attempt to load a real-mode program')
         else if hRet = 16 then
            raise Exception.Create('Attempt to load a second instance of an application with non-readonly data segments.')
         else if hRet = 19 then
            raise Exception.Create('Attempt to load a compressed application file.')
         else if hRet = 20 then
            raise Exception.Create('Dynamic-link library (DLL) file failure.')
         else if hRet = 26 then
            raise Exception.Create('A sharing violation occurred.')
         else if hRet = 27 then
            raise Exception.Create('The filename association is incomplete or invalid.')
         else if hRet = 28 then
            raise Exception.Create('The DDE transaction could not be completed because the request timed out.')
         else if hRet = 29 then
            raise Exception.Create('The DDE transaction failed.')
         else if hRet = 30 then
            raise Exception.Create('The DDE transaction could not be completed because other DDE transactions were being processed.')
         else if hRet = 31 then
            raise Exception.Create('There is no application associated with the given filename extension.')
         else if hRet = 32 then
            raise Exception.Create('Windows 95 only: The specified dynamic-link library was not found.');
      end;
   except on E: Exception do
      TErro.Executa(E.Message);
   end;
end;

end.
